package com.yz.lzq.liwushuo.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.yz.lzq.liwushuo.R;
import com.yz.lzq.liwushuo.activity.SearchActivity;
import com.yz.lzq.liwushuo.adapter.CategoryAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/11/12.
 */

public class CategoryFragment extends Fragment {
    @Bind(R.id.tablayout_fragment_category)
    TabLayout tablayoutFragmentCategory;
    @Bind(R.id.veiwPager_fragment_category)
    ViewPager viewPagerFragmentCategory;
    @Bind(R.id.button_fragment_category)
    Button buttonFragmentCategory;
    private Context mContext = null;
    private String[] arrTitles = {"攻略", "单品"};
    private List<Fragment> mList = new ArrayList<>();

    public static CategoryFragment getInstance() {
        CategoryFragment fragment = new CategoryFragment();
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getContext();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_category, container, false);
        ButterKnife.bind(this, view);
        buttonFragmentCategory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(mContext, SearchActivity.class);

                mContext.startActivity(intent);

            }
        });
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        CategoryDanPFragment categoryDanPFragment = new CategoryDanPFragment();
        CategoryGongLFragment categoryGongLFragment = new CategoryGongLFragment();
        mList.add(categoryGongLFragment);
        mList.add(categoryDanPFragment);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        CategoryAdapter adapter = new CategoryAdapter(getChildFragmentManager(), arrTitles, mList);
        viewPagerFragmentCategory.setAdapter(adapter);
        tablayoutFragmentCategory.setupWithViewPager(viewPagerFragmentCategory);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

//    @OnClick(R.id.button_fragment_category)
//    public void onClick() {
//        Toast.makeText(mContext, "--------------", Toast.LENGTH_SHORT).show();
//    }
}
